<?php

namespace app\common\traits;

use app\common\constant\Constants;

trait Output {

    public static function json($data)
    {
        return json($data);
    }

    public static function jsonResult($data = [], $msg = Constants::JSON_MESSAGE_SUCCESS, $code = Constants::JSON_STATUS_SUCCESS, $extra = [])
    {
        $msg = $msg ? $msg : Constants::JSON_MESSAGE_EMPTY;
        $result = ['code' => $code, 'msg' => $msg, 'data' => $data];
        $extra && $result = array_merge($result, $extra);
        # \think\Response::create($result, 'json')->send(); exit;
        return self::json($result);
    }

    public static function jsonSuccess($data = [], $extra = [], $msg = Constants::JSON_MESSAGE_SUCCESS)
    {
        return self::jsonResult($data, $msg, $code = Constants::JSON_STATUS_SUCCESS, $extra);
    }

    public static function jsonError($msg = Constants::JSON_MESSAGE_EMPTY, $code = Constants::JSON_STATUS_ERROR, $data = [])
    {
        $msg = $msg ? $msg : Constants::JSON_MESSAGE_EMPTY;
        $result = ['code' => $code, 'msg' => $msg, 'data' => $data];
        # \think\Response::create($result, 'json')->send(); exit;
        return self::json($result);
    }

}
